//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ImpliedTermStructure.h"
using namespace Cephei::QL::Termstructures::Yield;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/InterestRate.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Quote.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Yield::CImpliedTermStructure::CImpliedTermStructure (Cephei::QL::Termstructures::IYieldTermStructure^ prm1, DateTime referenceDate) : CYieldTermStructure(CImpliedTermStructure::typeid)
{
    CYieldTermStructure^ _Cprm1;
    try
    {
#ifdef HANDLE
        _phImpliedTermStructure = NULL;
#endif
        _Cprm1 = safe_cast<CYieldTermStructure^> (prm1);
        _Cprm1->Lock();
        Handle<QuantLib::YieldTermStructure>& _prm1 = static_cast<Handle<QuantLib::YieldTermStructure>&> (_Cprm1->GetHandle ()); 
        QuantLib::Date _referenceDate = (QuantLib::Date)ValueHelper::Convert (referenceDate); //d
        _ppImpliedTermStructure = new boost::shared_ptr<QuantLib::ImpliedTermStructure> (new QuantLib::ImpliedTermStructure ( _prm1,  _referenceDate ));
        SetYieldTermStructure (boost::dynamic_pointer_cast<QuantLib::YieldTermStructure> (*_ppImpliedTermStructure));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CImpliedTermStructure::CImpliedTermStructure (boost::shared_ptr<QuantLib::ImpliedTermStructure>& childNative, Object^ owner) : CYieldTermStructure(CImpliedTermStructure::typeid)
{
#ifdef HANDLE
	_phImpliedTermStructure = NULL;
#endif
	_ppImpliedTermStructure = &childNative;
    _ppYieldTermStructure = new boost::shared_ptr<QuantLib::YieldTermStructure> (boost::dynamic_pointer_cast<QuantLib::YieldTermStructure> (*_ppImpliedTermStructure));
}
Cephei::QL::Termstructures::Yield::CImpliedTermStructure::CImpliedTermStructure (QuantLib::ImpliedTermStructure& childNative, Object^ owner) : CYieldTermStructure(CImpliedTermStructure::typeid)
{
#ifdef HANDLE
	_phImpliedTermStructure = NULL;
#endif
	_ppImpliedTermStructure = new boost::shared_ptr<QuantLib::ImpliedTermStructure> (&childNative);
    _ppYieldTermStructure = new boost::shared_ptr<QuantLib::YieldTermStructure> (boost::dynamic_pointer_cast<QuantLib::YieldTermStructure> (*_ppImpliedTermStructure));
    _ImpliedTermStructureOwner = owner;
    _YieldTermStructureOwner = owner;
}

Cephei::QL::Termstructures::Yield::CImpliedTermStructure::CImpliedTermStructure (CImpliedTermStructure^ copy) : CYieldTermStructure(CImpliedTermStructure::typeid)
{
#ifdef HANDLE
	_phImpliedTermStructure = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppImpliedTermStructure = new boost::shared_ptr<QuantLib::ImpliedTermStructure> (copy->GetShared());
        _ppYieldTermStructure = new boost::shared_ptr<QuantLib::YieldTermStructure> (boost::dynamic_pointer_cast<QuantLib::YieldTermStructure> (*_ppImpliedTermStructure));
    }
}
Cephei::QL::Termstructures::Yield::CImpliedTermStructure::CImpliedTermStructure (PLATFORM::Type^ t) : CYieldTermStructure(CImpliedTermStructure::typeid)
{
#ifdef HANDLE
	_phImpliedTermStructure = NULL;
#endif
	if (!t->IsSubclassOf(CImpliedTermStructure::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::CImpliedTermStructure::CImpliedTermStructure (QuantLib::Handle<QuantLib::ImpliedTermStructure>& childNative, Object^ owner)  : CYieldTermStructure(CImpliedTermStructure::typeid)
{
	_phImpliedTermStructure = &childNative;
	_ppImpliedTermStructure = &static_cast<boost::shared_ptr<QuantLib::ImpliedTermStructure>>(childNative.currentLink());
    _ppYieldTermStructure = new boost::shared_ptr<QuantLib::YieldTermStructure> (boost::dynamic_pointer_cast<QuantLib::YieldTermStructure> (*_ppImpliedTermStructure));
    _ImpliedTermStructureOwner = owner;
}
Cephei::QL::Termstructures::Yield::CImpliedTermStructure::CImpliedTermStructure (QuantLib::Handle<QuantLib::ImpliedTermStructure> childNative)  : CYieldTermStructure(CImpliedTermStructure::typeid)
{
	_phImpliedTermStructure = &childNative;
	_ppImpliedTermStructure = &static_cast<boost::shared_ptr<QuantLib::ImpliedTermStructure>>(childNative.currentLink());
    _ppYieldTermStructure = new boost::shared_ptr<QuantLib::YieldTermStructure> (boost::dynamic_pointer_cast<QuantLib::YieldTermStructure> (*_ppImpliedTermStructure));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::CImpliedTermStructure::CImpliedTermStructure (QuantLib::ImpliedTermStructure childNative)  : CYieldTermStructure(CImpliedTermStructure::typeid)
{
#ifdef HANDLE
	_phImpliedTermStructure = NULL;
#endif
	_ppImpliedTermStructure = new boost::shared_ptr<QuantLib::ImpliedTermStructure> (new QuantLib::ImpliedTermStructure (childNative));
    _ppYieldTermStructure = new boost::shared_ptr<QuantLib::YieldTermStructure> (boost::dynamic_pointer_cast<QuantLib::YieldTermStructure> (*_ppImpliedTermStructure));
}
#endif

Cephei::QL::Termstructures::Yield::CImpliedTermStructure::~CImpliedTermStructure ()
{
    if (_ppImpliedTermStructure != NULL)
    {
	    delete _ppImpliedTermStructure;
        _ppImpliedTermStructure = NULL;
    }
}
Cephei::QL::Termstructures::Yield::CImpliedTermStructure::!CImpliedTermStructure ()
{
    if (_ppImpliedTermStructure != NULL)
    {
	    delete _ppImpliedTermStructure;
    }
}
QuantLib::ImpliedTermStructure& Cephei::QL::Termstructures::Yield::CImpliedTermStructure::GetReference ()
{
    if (_ppImpliedTermStructure == NULL) throw REFNEW NativeNullException ();
	return **_ppImpliedTermStructure;
}
boost::shared_ptr<QuantLib::ImpliedTermStructure>& Cephei::QL::Termstructures::Yield::CImpliedTermStructure::GetShared ()
{
    if (_ppImpliedTermStructure == NULL) throw REFNEW NativeNullException ();
	return *_ppImpliedTermStructure;
}
QuantLib::ImpliedTermStructure* Cephei::QL::Termstructures::Yield::CImpliedTermStructure::GetPointer ()
{
    if (_ppImpliedTermStructure == NULL) throw REFNEW NativeNullException ();
	return &**_ppImpliedTermStructure;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ImpliedTermStructure>& Cephei::QL::Termstructures::Yield::CImpliedTermStructure::GetHandle ()
{
	if (_phImpliedTermStructure == NULL)
	{
		_phImpliedTermStructure = new Handle<QuantLib::ImpliedTermStructure> (*_ppImpliedTermStructure);
	}
	return *_phImpliedTermStructure;
}
#endif
bool Cephei::QL::Termstructures::Yield::CImpliedTermStructure::HasNative () 
{
	return (_ppImpliedTermStructure != NULL);
}

Cephei::QL::Times::ICalendar^ Cephei::QL::Termstructures::Yield::CImpliedTermStructure::Calendar::get ()
{
    try
    {
    	QuantLib::Calendar _rv = (QuantLib::Calendar)(*_ppImpliedTermStructure)->calendar ( );   
        Cephei::QL::Times::CCalendar^ _nrv = REFNEW Cephei::QL::Times::CCalendar (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Yield::CImpliedTermStructure::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppImpliedTermStructure)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = REFNEW Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Yield::CImpliedTermStructure::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppImpliedTermStructure)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt32 Cephei::QL::Termstructures::Yield::CImpliedTermStructure::SettlementDays::get ()
{
    try
    {
    	QuantLib::Natural _rv = (QuantLib::Natural)(*_ppImpliedTermStructure)->settlementDays ( );   
        UInt32 _nrv = (UInt32)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Yield::IImpliedTermStructure^ Cephei::QL::Termstructures::Yield::CImpliedTermStructure_Factory::Create (Cephei::QL::Termstructures::IYieldTermStructure^ prm1, DateTime referenceDate)
{
    return REFNEW CImpliedTermStructure ( prm1,  referenceDate);
}
